import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class testdbzuoye {
    private String user;
    private String password;
    private String bd;
    private Connection connection;

    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        testdbzuoye testdbzuoye = new testdbzuoye();
    }
    public testdbzuoye() throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/jdbc.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Class.forName(driver);
        connection = DriverManager.getConnection(url, user, password);
    }
    public testdbzuoye(String user,String password,String bd) throws ClassNotFoundException, SQLException {
        this.user=user;
        this.password=password;
        this.bd=bd;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC",
                    this.user,this.password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void insert(String sql)  throws ClassNotFoundException, SQLException {
        //3.实现Crud
//        String sql = "";
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,"我同学");
        statement.setInt(2,16);
        statement.setString(3,"女");
        statement.setString(4,"信管");
        statement.setString(5,"100.0");

        //5.执行sql,获取结果集
        int i = statement.executeUpdate();
        System.out.println("插入 " + i);
        //6.关闭资源
        statement.close();
        connection.close();
    }
    public void delate(String sql)  throws ClassNotFoundException, SQLException {
        //3.实现Crud
//        String sql = "";
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,"我同学");
        //5.执行sql,获取结果集
        int i = statement.executeUpdate();
        System.out.println("删除 " + i);
        //6.关闭资源
        statement.close();
        connection.close();
    }
    public void update(String sql)  throws ClassNotFoundException, SQLException {
        //3.实现Crud
//        String sql = "";
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setString(1,"计算机");
        statement.setString(2,"我同学");
//fule


        //5.执行sql,获取结果集
        int i = statement.executeUpdate();
        System.out.println("更新 " + i);
        //6.关闭资源
        statement.close();
        connection.close();
    }
    public void select(String sql)  throws ClassNotFoundException, SQLException {
//        //3.实现Crud
        //String sql = "select *from stu2";
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        //5.执行sql,获取结果集
        ResultSet resultSet = statement.executeQuery();
        //6.通过next指向结果集第一行
        while (resultSet.next()) {
            //7.解析结果中数据字段
            System.out.println("name = " + resultSet.getString("name"));
            System.out.println("age = " + resultSet.getInt("age"));
            System.out.println("sex = " + resultSet.getString("sex"));
            System.out.println("major = " + resultSet.getString("major"));
            System.out.println("score = " + resultSet.getString("score"));
        }
        //8.关闭资源
        statement.close();
        connection.close();
    }

}
